package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.GivenPublicNotice;
import com.youedata.modular.model.dto.GivenPublicNoticeDTO;
import com.youedata.modular.service.IGivenPublicNoticeService;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知公告表控制器
 *
 * @author 炎之十月
 * @date 2021-08-06 22:08:40
 */
@Controller
@Api(value = "/givenPublicNotice", tags = "通知公告表管理")
@RequestMapping("/givenPublicNotice")
public class GivenPublicNoticeController {

    private String PREFIX = "/givenPublicNotice";

    @Autowired
    private IGivenPublicNoticeService givenPublicNoticeService;

    /**
     * 新增
     *
     * @author 炎之十月
     * @date 2021-08-06
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(GivenPublicNoticeDTO givenPublicNoticeDto) {
        GivenPublicNotice givenPublicNotice = new GivenPublicNotice();
        BeanUtils.copyProperties(givenPublicNoticeDto, givenPublicNotice);
        givenPublicNotice.setCreateUser(Long.valueOf(UserHolder.getUserId()));
        givenPublicNotice.setCreateTime(LocalDateTime.now());
        this.givenPublicNoticeService.save(givenPublicNotice);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author 炎之十月
     * @date 2021-08-06
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.givenPublicNoticeService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author 炎之十月
     * @date 2021-08-06
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(GivenPublicNoticeDTO givenPublicNoticeDto) {
        GivenPublicNotice givenPublicNotice = new GivenPublicNotice();
        BeanUtils.copyProperties(givenPublicNoticeDto, givenPublicNotice);
        givenPublicNotice.setUpdateUser(String.valueOf(UserHolder.getUserId()));
        givenPublicNotice.setUpdateTime(LocalDateTime.now());
        this.givenPublicNoticeService.updateById(givenPublicNotice);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键查询单条详情
     *
     * @author 炎之十月
     * @date 2021-08-06
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") String id) {
        return ResponseData.success(this.givenPublicNoticeService.getById(id));
    }

    /**
     * 分页查询列表
     *
     * @author 炎之十月
     * @date 2021-08-06
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(GivenPublicNoticeDTO givenPublicNoticeDto) {
        GivenPublicNotice givenPublicNotice = new GivenPublicNotice();
        givenPublicNoticeDto.setName(StringUtils.isEmpty(givenPublicNoticeDto.getName())?null:givenPublicNoticeDto.getName());
        givenPublicNoticeDto.setType(StringUtils.isEmpty(givenPublicNoticeDto.getType())?null:givenPublicNoticeDto.getType());
        BeanUtils.copyProperties(givenPublicNoticeDto, givenPublicNotice);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询
//        page.setRecords(this.givenPublicNoticeService.page(page, Wrappers.query(givenPublicNotice)).getRecords());
          //  page.setRecords(this.givenPublicNoticeService.queryFuzzySearch(page,givenPublicNotice));
        List<GivenPublicNotice> givenPublicNoticeList = this.givenPublicNoticeService.queryFuzzySearch(page, givenPublicNotice);
        if (ObjectUtils.isNotEmpty(givenPublicNoticeList)) {
            givenPublicNoticeList.stream().map(x -> {
                if (ToolUtil.isNotEmpty(x.getCreateTime())) {
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                    x.setCreateTimeStr(dtf.format(x.getCreateTime()));
                }
                return x;
            }).collect(Collectors.toList());
        }
        page.setRecords(givenPublicNoticeList);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author 炎之十月
     * @Date 2021-08-06
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/givenPublicNotice.html";
    }

    /**
     * 新增页面
     *
     * @author 炎之十月
     * @Date 2021-08-06
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/givenPublicNotice_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 炎之十月
     * @Date 2021-08-06
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/givenPublicNotice_edit.html";
    }


    @RequestMapping("/zcjd")
    public String zcjdIndex() {
        return PREFIX + "/zcjd/givenPublicNoticeZcjd.html";
    }
    @RequestMapping("/zcjd/add")
    public String zcjdAdd() {
        return PREFIX + "/zcjd/givenPublicNoticeZcjd_add.html";
    }
    @RequestMapping("/zcjd/edit")
    public String zcjdEdit() {
        return PREFIX + "/zcjd/givenPublicNoticeZcjd_edit.html";
    }

    @RequestMapping("/qyyj")
    public String qyyjIndex() {
        return PREFIX + "/qyyj/givenPublicNoticeQyyj.html";
    }
    @RequestMapping("/qyyj/add")
    public String qyyjAdd() {
        return PREFIX + "/qyyj/givenPublicNoticeQyyj_add.html";
    }
    @RequestMapping("/qyyj/edit")
    public String qyyjEdit() {
        return PREFIX + "/qyyj/givenPublicNoticeQyyj_edit.html";
    }

    @RequestMapping("/zjfk")
    public String zjfkIndex() {
        return PREFIX + "/zjfk/givenPublicNoticeZjfk.html";
    }
    @RequestMapping("/zjfk/add")
    public String zjfkAdd() {
        return PREFIX + "/zjfk/givenPublicNoticeZjfk_add.html";
    }
    @RequestMapping("/zjfk/edit")
    public String zjfkEdit() {
        return PREFIX + "/zjfk/givenPublicNoticeZjfk_edit.html";
    }

    @RequestMapping("/cyfc")
    public String cyfcIndex() {
        return PREFIX + "/cyfc/givenPublicNoticeCyfc.html";
    }
    @RequestMapping("/cyfc/add")
    public String cyfcAdd() {
        return PREFIX + "/cyfc/givenPublicNoticeCyfc_add.html";
    }
    @RequestMapping("/cyfc/edit")
    public String cyfcEdit() {
        return PREFIX + "/cyfc/givenPublicNoticeCyfc_edit.html";
    }


}
